# 短信下发模块
import random
from django.core.cache import cache
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from django.shortcuts import HttpResponse


def test_redis(request):
    # 存储数据
    cache.set('name','tom',20) #该值的有效期为20s
    # 判断redis 中是否存在
    print(cache.has_kay('name')) # 包含true
    # 获取
    print(cache.get("name")) #返回:tom 无返回Null
    return  HttpResponse('测试redis')

def send_sms(phone,code):
    client = AcsClient('LTAIhWZhJcOF3FXz','jZIvqlwlZv6izzxLhBTUtEDT4iP2Rt','cn-hangzhou')

    code = "{'code':%s}" % (code)
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', phone)
    request.add_query_param('SignName', "腾龙国际")
    request.add_query_param('TemplateCode', "SMS_172205415")
    request.add_query_param('TemplateParam', code)

    response = client.do_action(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    return str(response, encoding='utf-8')

#数字表示生成几位, True表示生成带有字母的 False不带字母的

def get_code(n=6,alpha= True):
    s = '' # 创建字符串变量,存储生成的验证码
    for i in range(n):# 通过for循环控制验证码位数
        num = random.randint(0,9) # 生成随机数字0-9
        if alpha: # 需要字母验证码,不用传参,如果不需要字母的,关键字alpha=False
            upper_alpha = chr(random.randint(65,90))
            lower_alpha = chr(random.randint(97,122))
            num = random.choice([num,upper_alpha,lower_alpha])
        s = s + str(num)

    return s
if __name__ == '__main__':
    send_sms('15635908215', get_code(6,False))
    print(get_code(6,False))  # 打印6位数字验证码
    print(get_code(6,True)) # 打印6位数字字母混合验证码
    print(get_code(4,False)) # 打印4位数字验证码
    print(get_code(4,True)) # 打印4位数字字母混合验证码



